package com.android.memoprendiz.controller;

import com.android.memoprendiz.R;
import com.android.memoprendiz.model.Configuration;
import com.android.memoprendiz.model.ConfigurationsDAO;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class ConfiguracaoActivity extends Activity {
    private RadioGroup nivel;
    private RadioGroup tema;

    
    public void onResume(){
    	super.onResume();
        
        ConfigurationsDAO CD = new ConfigurationsDAO(this);
        Configuration config = CD.getConfiguration();
        int id = this.getResources().getIdentifier(config.getCaminhoEstilo(), "style",this.getPackageName());
        this.setTheme(id);
        
        switch(config.getNivelInicialNum()){
    	case 1:
            this.nivel.check(R.id.nivel1);
            break;
    	case 2:
    		this.nivel.check(R.id.nivel2);
    		break;
    	case 3:
    		this.nivel.check(R.id.nivel3);
    		break;
    	default:
    		this.nivel.check(R.id.nivel1);
    }
        
    }
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        ConfigurationsDAO CD = new ConfigurationsDAO(this);
        Configuration config = CD.getConfiguration();
        int id = this.getResources().getIdentifier(config.getCaminhoEstilo(), "style",this.getPackageName());
        this.setTheme(id);
        
        setContentView(R.layout.configuracao);
        
        this.nivel = (RadioGroup) findViewById(R.id.grupoNivel);
        

        
        
        switch(config.getNivelInicialNum()){
        	case 1:
                this.nivel.check(R.id.nivel1);
                break;
        	case 2:
        		this.nivel.check(R.id.nivel2);
        		break;
        	case 3:
        		this.nivel.check(R.id.nivel3);
        		break;
        	default:
        		this.nivel.check(R.id.nivel1);
        }
        
        
        
        
        
        this.tema = (RadioGroup) findViewById(R.id.grupoTema);
        String nomeTema = config.getCaminhoEstilo();
        if(nomeTema.equals("Defaults")){
        	this.tema.check(R.id.padrao);
        }else if(nomeTema.equals("Blue")){
        	this.tema.check(R.id.azul);
        }else if(nomeTema.equals("Green")){
        	this.tema.check(R.id.verde);
        }else if(nomeTema.equals("Vermelho")){
        	this.tema.check(R.id.vermelho);
        }
        
    }

	@Override
	public void finish() 	{
		Bundle bundle = new Bundle();
		
		int id;
		RadioButton btn;
		
		id = this.nivel.getCheckedRadioButtonId();
		btn = (RadioButton) findViewById(id);
		String valor = btn.getText().toString();
		bundle.putString("nivel", "n"+valor.charAt(valor.length() - 1));
		
		id = this.tema.getCheckedRadioButtonId();
		btn = (RadioButton) findViewById(id);
		valor = btn.getText().toString();
		
		
		if(valor.equals(getString(R.string.tm_padrao))){
			valor = "Default";
		}else if(valor.equals(getString(R.string.tm_azul))){
			valor = "Blue";
		}else if(valor.equals(getString(R.string.tm_verde))){
			valor = "Green";
		}else if(valor.equals(getString(R.string.tm_vermelho))){
			valor = "Red";
		}
		
		
		bundle.putString("tema", valor);

        Intent it = new Intent();
        it.putExtras(bundle);
        setResult(1, it);
    	
        super.finish();
	}
	
    public void eventoBotaoFrase(View v){
    	startActivity(new Intent(getApplicationContext(), FraseConfigActivity.class));
    }
}